home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / UpdateManager / GtkProgress.py < prev    next >
Text File  |  2009-11-02  |  6KB  |  156 lines

  1. # GtkProgress.py 
  2. #  
  3. #  Copyright (c) 2004,2005 Canonical
  4. #  
  5. #  Author: Michael Vogt <michael.vogt@ubuntu.com>
  6. #  This program is free software; you can redistribute it and/or 
  7. #  modify it under the terms of the GNU General Public License as 
  8. #  published by the Free Software Foundation; either version 2 of the
  9. #  License, or (at your option) any later version.
  10. #  This program is distributed in the hope that it will be useful,
  11. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #  GNU General Public License for more details.
  14. #  You should have received a copy of the GNU General Public License
  15. #  along with this program; if not, write to the Free Software
  16. #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. #  USA
  18.  
  19. import pygtk
  20. pygtk.require('2.0')
  21. import gtk
  22. import apt
  23. import apt_pkg
  24. from gettext import gettext as _
  25. from Core.utils import *
  26.  
  27. # intervals of the start up progress
  28. # 3x caching and menu creation
  29. STEPS_UPDATE_CACHE = [33, 66, 100]
  30.  
  31. class GtkOpProgress(apt.OpProgress):
  32.     def __init__(self, host_window, progressbar, status, parent,
  33.                  steps=STEPS_UPDATE_CACHE):
  34.         # used for the "one run progressbar"
  35.         self.steps = steps[:]
  36.         self.base = 0
  37.         self.old = 0
  38.         self.next = int(self.steps.pop(0))
  39.  
  40.         self._parent = parent
  41.         self._window = host_window
  42.         self._status = status
  43.         self._progressbar = progressbar
  44.         # Do not show the close button 
  45.         self._window.realize()
  46.         host_window.window.set_functions(gtk.gdk.FUNC_MOVE)
  47.         self._window.set_transient_for(parent)
  48.  
  49.     def update(self, percent):
  50.         #print percent
  51.         #print self.Op
  52.         #print self.SubOp
  53.         # only show progress bar if the parent is not iconified (#353195)
  54.         state = self._parent.window.get_state()
  55.         if not (state  & gtk.gdk.WINDOW_STATE_ICONIFIED):
  56.             self._window.show()
  57.         self._parent.set_sensitive(False)
  58.         # if the old percent was higher, a new progress was started
  59.         if self.old > percent:
  60.             # set the borders to the next interval
  61.             self.base = self.next
  62.             try:
  63.                 self.next = int(self.steps.pop(0))
  64.             except:
  65.                 pass
  66.         progress = self.base + percent/100 * (self.next - self.base)
  67.         self.old = percent
  68.         self._status.set_markup("<i>%s</i>" % self.op)
  69.         self._progressbar.set_fraction(progress/100.0)
  70.         while gtk.events_pending():
  71.             gtk.main_iteration()
  72.  
  73.     def done(self):
  74.         self._parent.set_sensitive(True)
  75.     def hide(self):
  76.         self._window.hide()
  77.  
  78. class GtkFetchProgress(apt.progress.FetchProgress):
  79.     def __init__(self, parent, summary="", descr=""):
  80.         # if this is set to false the download will cancel
  81.         self._continue = True
  82.         # init vars here
  83.         # FIXME: find a more elegant way, this sucks
  84.         self.summary = parent.label_fetch_summary
  85.         self.status = parent.label_fetch_status
  86.         # we need to connect the signal manual here, it won't work
  87.         # from the main window auto-connect
  88.         parent.button_fetch_cancel.connect(
  89.             "clicked", self.on_button_fetch_cancel_clicked)
  90.         self.progress = parent.progressbar_fetch
  91.         self.window_fetch = parent.window_fetch
  92.         self.window_fetch.set_transient_for(parent.window_main)
  93.         self.window_fetch.realize()
  94.         self.window_fetch.window.set_functions(gtk.gdk.FUNC_MOVE)
  95.         # set summary
  96.         if summary != "":
  97.             self.summary.set_markup("<big><b>%s</b></big> \n\n%s" %
  98.                                     (summary, descr))
  99.     def start(self):
  100.         self.progress.set_fraction(0)
  101.         self.window_fetch.show()
  102.     def stop(self):
  103.         self.window_fetch.hide()
  104.     def on_button_fetch_cancel_clicked(self, widget):
  105.         self._continue = False
  106.     def pulse(self):
  107.         apt.progress.FetchProgress.pulse(self)
  108.         currentItem = self.currentItems + 1
  109.         if currentItem > self.totalItems:
  110.           currentItem = self.totalItems
  111.         if self.currentCPS > 0:
  112.             statusText = (_("Downloading file %(current)li of %(total)li with "
  113.                             "%(speed)s/s") % {"current" : currentItem,
  114.                                               "total" : self.totalItems,
  115.                                               "speed" : humanize_size(self.currentCPS)})
  116.         else:
  117.             statusText = (_("Downloading file %(current)li of %(total)li") % \
  118.                           {"current" : currentItem,
  119.                            "total" : self.totalItems })
  120.             self.progress.set_fraction(self.percent/100.0)
  121.         self.status.set_markup("<i>%s</i>" % statusText)
  122.         # TRANSLATORS: show the remaining time in a progress bar:
  123.         #self.progress.set_text(_("About %s left" % (apt_pkg.TimeToStr(self.eta))))
  124.     # FIXME: show remaining time
  125.         self.progress.set_text("")
  126.  
  127.         while gtk.events_pending():
  128.             gtk.main_iteration()
  129.         return self._continue
  130.  
  131. if __name__ == "__main__":
  132.     import apt
  133.     import apt_pkg
  134.     from SimpleGtkbuilderApp import SimpleGtkbuilderApp
  135.  
  136.     class MockParent(SimpleGtkbuilderApp):
  137.         """Mock parent for the fetcher that just loads the UI file"""
  138.         def __init__(self):
  139.             SimpleGtkbuilderApp.__init__(self, "../data/glade/UpdateManager.ui")
  140.  
  141.     # create mock parent and fetcher
  142.     parent = MockParent()
  143.     fetch_progress = GtkFetchProgress(parent, "summary", "long detailed description")
  144.  
  145.     # generate a dist-upgrade (to feed data to the fetcher) and get it
  146.     cache = apt.Cache()
  147.     cache.upgrade()
  148.     pm = apt_pkg.GetPackageManager(cache._depcache)
  149.     fetcher = apt_pkg.GetAcquire(fetch_progress)
  150.     cache._fetchArchives(fetcher, pm)
  151.     
  152.     
  153.